package com.udata.client.view.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import com.udata.client.place.DashboardPlace;
import com.udata.client.place.UserRegisterPlace;
import com.udata.client.service.UserService;
import com.udata.client.service.UserServiceAsync;
import com.udata.client.view.UserView;
import com.udata.shared.CookiesConfig;
import com.udata.shared.conf.TokenConfig;
import com.udata.shared.exception.OperationException;
import com.udata.shared.model.UserB;

public class UserViewImpl extends Composite implements UserView {
  private static UserViewImplUiBinder uiBinder = GWT.create(UserViewImplUiBinder.class);

  interface UserViewImplUiBinder extends UiBinder<Widget, UserViewImpl> {
  }

  UserServiceAsync instanceService = (UserServiceAsync) GWT.create(UserService.class);

  interface Style extends CssResource {

  }

  @UiField
  Style             style;

  @UiField
  TextBox           emailTextBox;
  @UiField
  TextBox           passwordTextBox;

  @UiField
  HTMLPanel         mainPanel;

  @UiField
  HTMLPanel         headerPanel;

  @UiField
  HTMLPanel         loginPanel;

  @UiField
  Label             loginStatus;

  @UiField
  Button            loginButton;
  @UiField
  Button            registerButton;

  private Presenter presenter;

  public UserViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
    this.initHeaderPanel();
    loginStatus.setText("");
  }

  @UiHandler("registerButton")
  public void clickRegisterButton(ClickEvent clickEvent) {
    presenter.goTo(new UserRegisterPlace(TokenConfig.USER_REGISTER));
  }

  private void initHeaderPanel() {
    headerPanel.clear();
    String userName = Cookies.getCookie("udata_user");
    if (userName == null) {
      headerPanel.add(registerButton);
    } else {
      presenter.goTo(new DashboardPlace(TokenConfig.DASHBOARD));
    }
  }

  @UiHandler("loginButton")
  public void clickLoginButton(ClickEvent clickEvent) {
    String email = emailTextBox.getText();
    String password = passwordTextBox.getText();
    
    instanceService.login(email, password, new AsyncCallback<UserB>() {

      @Override
      public void onFailure(Throwable caught) {
        GWT.log(caught.getLocalizedMessage());
        loginStatus.setText("The username or password you entered is incorrect.");
      }

      @Override
      public void onSuccess(UserB result) {
        GWT.log("Success");
        if (result == null) {
          loginStatus.setText("The username or password you entered is incorrect.");
        } else {
          Cookies.setCookie(CookiesConfig.USERNAME, result.getName());
          Cookies.setCookie(CookiesConfig.EMAIL, result.getEmail());

          presenter.goTo(new DashboardPlace("dashboard"));
        }
      }
    });
  }

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

}